package droid.frame.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * 让ImageView的高度、与其父亲的宽高一样
 * 
 * @author coffee<br>
 *         2017年6月29日下午2:16:06
 */
@SuppressLint("AppCompatCustomView")
public class MatchParentImageView extends ImageView {

	public MatchParentImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		Drawable d = getDrawable();
		if (d != null) {
			// ceil not round - avoid thin vertical gaps along the left/right edges
			// int width = MeasureSpec.getSize(widthMeasureSpec);
			// int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
			// setMeasuredDimension(width, height);
			int width = getMeasuredWidth();
			ViewGroup parent = ((ViewGroup) getParent());
			int height = parent.getHeight();
			widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
			// 精确计算高度
			heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		} else {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}
	}

}